package com.tanhua.server.controller;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.UserInfoVo;
import com.tanhua.server.service.UserService;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/users")
public class UserInfoController {

//    1、请求方式  get
//    2、url地址  /users
//    3、请求参数  application/json   有请求头Authorization
//    Query参数：userID  huanxinID
//    4、返回结果  userInfoVo
    @Autowired
    private UserService userService;

    @GetMapping(name = "获取用户资料")
    public ResponseEntity queryUser(Long userID,Long huanxinID,@RequestHeader("Authorization") String token){
//       userID--->huanxinID---->token
        Long id = null;
        if(userID!=null){
            id = userID;
        }else{
            if(huanxinID!=null){
                id = huanxinID;
            }else{
               Long userId = userService.getUserByToken(token).getId();
                id = userId;
            }
        }
        return userService.findById(id);
    }


    @PutMapping(name="修改个人资料")
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){

        return userService.updateUserInfo(userInfo);
    }

    @PostMapping(value = "/header",name="修改个人头像")
    public ResponseEntity updateHeader(@RequestHeader("Authorization") String token, MultipartFile headPhoto) throws Exception{
        return userService.loginReginfoHead(headPhoto,token);
    }

}
